package table.view.multirowheader;
import java.awt.Dimension;
import java.util.Iterator;

import javax.swing.table.JTableHeader;

import table.MultiLineTable;
import table.model.ColumnGroup;


/**
 * This is the object which manages the header of the JTable and
 * also provides functionality for groupable headers.
 * 
 * @author D'Er'Nasherbrezon aka dernasherbrezon %)
 */
public class GroupableTableHeader extends JTableHeader
{
	MultiLineTable table;
    /**
     * Constructs a GroupableTableHeader which is initialized with cm as the
     * column model. If cm is null this method will initialize the table header
     * with a default TableColumnModel.
     * @param model the column model for the table
     */    
    public GroupableTableHeader(MultiLineTable table,GroupableTableColumnModel model)
    {
        super(model);
        this.table = table;
        setUI(new GroupableTableHeaderUI(table));
        setReorderingAllowed(false);
    }
    
    
    /**
     * Sets the margins correctly for all groups within
     * the header.
     */    
    public void setColumnMargin()
    {
        int columnMargin = getColumnModel().getColumnMargin();
        Iterator iter = ((GroupableTableColumnModel)columnModel).columnGroupIterator();
        while (iter.hasNext())
        {
        	ColumnGroup cGroup = (ColumnGroup)iter.next();
            cGroup.setColumnMargin(columnMargin);
        }
    }


    @Override
    public Dimension getPreferredSize()
    {
    	Dimension defaultPrefSize = getParentPreferredSize();
    	int maxHeaderHeight = table.getMaxHeaderHeight();
    	defaultPrefSize.height = maxHeaderHeight;
    	return defaultPrefSize;
    }
    
	public Dimension getParentPreferredSize()
	{
		return super.getPreferredSize();
	}
}

